package com.sfy.lotterysystem.controller;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.ArrayList;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sfy.lotterysystem.entity.Prize;
import com.sfy.lotterysystem.entity.User;
import com.sfy.lotterysystem.entity.WinRecord;
import com.sfy.lotterysystem.repository.PrizeRepository;
import com.sfy.lotterysystem.repository.UserRepository;
import com.sfy.lotterysystem.repository.WinRecordRepository;
import com.sfy.lotterysystem.dto.WinRecordDTO;
@RestController
@RequestMapping("/api/lottery")
public class LotteryController {

    @Autowired
    private PrizeRepository prizeRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private WinRecordRepository winRecordRepository;
    @Autowired
    private AmqpTemplate amqpTemplate;

    @PostMapping("/draw")
    public Map<String, Object> draw(@RequestParam String username) {
        Map<String, Object> result = new HashMap<>();
        User user = userRepository.findByUsername(username);
        if (user == null) {
            result.put("success", false);
            result.put("message", "用户不存在");
            return result;
        }
        List<Prize> prizes = prizeRepository.findAll();
        if (prizes.isEmpty()) {
            result.put("success", false);
            result.put("message", "没有奖品");
            return result;
        }
        // 简单随机抽奖
        Prize prize = prizes.get(new Random().nextInt(prizes.size()));
        if (prize.getCount() > 0) {
            prize.setCount(prize.getCount() - 1);
            prizeRepository.save(prize);
            WinRecord record = new WinRecord();
            record.setUserId(user.getId());
            record.setPrizeId(prize.getId());
            record.setWinTime(new Date());
            winRecordRepository.save(record);
            result.put("success", true);
            result.put("message", "恭喜中奖：" + prize.getName());
            // 发送中奖消息到RabbitMQ
            amqpTemplate.convertAndSend("lottery.result", "用户" + user.getUsername() + " 中奖：" + prize.getName());
        } else {
            result.put("success", false);
            result.put("message", "很遗憾，未中奖");
            // 发送未中奖消息到RabbitMQ
            amqpTemplate.convertAndSend("lottery.result", "用户" + user.getUsername() + " 未中奖");
        }
        return result;
    }
    @GetMapping("/my-wins")
    public List<WinRecordDTO> myWins(@RequestParam String username) {
        User user = userRepository.findByUsername(username);
        if (user == null) return Collections.emptyList();
        List<WinRecord> records = winRecordRepository.findByUserId(user.getId());
        List<WinRecordDTO> result = new ArrayList<>();
        for (WinRecord record : records) {
            Prize prize = prizeRepository.findById(record.getPrizeId()).orElse(null);
            String prizeName = prize != null ? prize.getName() : "未知奖品";
            result.add(new WinRecordDTO(record.getId(), prizeName, record.getWinTime()));
        }
        return result;
    }
}